// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Collections.Generic;
using System.Linq;

// Factory methods in generic types are acceptable for object creation patterns
#pragma warning disable CA1000 // Do not declare static members on generic types

namespace MatrixFramework.Core.Results;

/// <summary>
/// 分页结果类
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
public class PagedResult<T> : ResultBase
{
    /// <summary>
    /// 数据列表
    /// </summary>
    public IReadOnlyList<T> Items { get; private set; } = new List<T>();

    /// <summary>
    /// 总记录数
    /// </summary>
    public long TotalCount { get; private set; }

    /// <summary>
    /// 当前页码（从1开始）
    /// </summary>
    public int Page { get; private set; }

    /// <summary>
    /// 每页大小
    /// </summary>
    public int PageSize { get; private set; }

    /// <summary>
    /// 总页数
    /// </summary>
    public int TotalPages => (int)Math.Ceiling((double)TotalCount / PageSize);

    /// <summary>
    /// 是否有下一页
    /// </summary>
    public bool HasNextPage => Page < TotalPages;

    /// <summary>
    /// 是否有上一页
    /// </summary>
    public bool HasPreviousPage => Page > 1;

    /// <summary>
    /// 私有构造函数
    /// </summary>
    private PagedResult() { }

    /// <summary>
    /// 创建成功分页结果
    /// </summary>
    /// <param name="items">数据列表</param>
    /// <param name="totalCount">总记录数</param>
    /// <param name="page">当前页码</param>
    /// <param name="pageSize">每页大小</param>
    /// <returns>分页结果</returns>
    public static PagedResult<T> Success(IEnumerable<T> items, long totalCount, int page, int pageSize)
    {
        return new PagedResult<T>
        {
            IsSuccess = true,
            Items = items?.ToList() ?? new List<T>(),
            TotalCount = totalCount,
            Page = page,
            PageSize = pageSize
        };
    }

    /// <summary>
    /// 创建成功分页结果（从查询结果）
    /// </summary>
    /// <param name="queryable">查询结果</param>
    /// <param name="page">当前页码</param>
    /// <param name="pageSize">每页大小</param>
    /// <returns>分页结果</returns>
    public static PagedResult<T> Success(IQueryable<T> queryable, int page, int pageSize)
    {
        var totalCount = queryable.Count();
        var items = queryable.Skip((page - 1) * pageSize).Take(pageSize).ToList();

        return Success(items, totalCount, page, pageSize);
    }

    /// <summary>
    /// 创建失败分页结果
    /// </summary>
    /// <param name="error">错误信息</param>
    /// <returns>分页结果</returns>
    public static PagedResult<T> Failure(string error)
    {
        var result = new PagedResult<T>
        {
            IsSuccess = false,
            TotalCount = 0,
            Page = 1,
            PageSize = 10
        };
        result.AddError(error);
        return result;
    }

    /// <summary>
    /// 创建失败分页结果
    /// </summary>
    /// <param name="errors">错误信息列表</param>
    /// <returns>分页结果</returns>
    public static PagedResult<T> Failure(IEnumerable<string> errors)
    {
        var result = new PagedResult<T>
        {
            IsSuccess = false,
            TotalCount = 0,
            Page = 1,
            PageSize = 10
        };
        result.AddErrors(errors ?? new List<string>());
        return result;
    }

    /// <summary>
    /// 创建空分页结果
    /// </summary>
    /// <param name="page">当前页码</param>
    /// <param name="pageSize">每页大小</param>
    /// <returns>空分页结果</returns>
    public static PagedResult<T> Empty(int page = 1, int pageSize = 10)
    {
        return new PagedResult<T>
        {
            IsSuccess = true,
            Items = new List<T>(),
            TotalCount = 0,
            Page = page,
            PageSize = pageSize
        };
    }
}

#pragma warning restore CA1000 // Do not declare static members on generic types